package MeteoCal.bean;

import MeteoCal.entity.Schedule;
import MeteoCal.entity.User;
import MeteoCal.manager.UserManager;
import MeteoCal.utility.Mail;
import java.io.Serializable;
import java.util.Date;
import java.util.Properties;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.primefaces.context.RequestContext;

/**
 *
 * @authors Guttadoro, Iommazzo, Saullo
 */
@ManagedBean
@SessionScoped
public class RegistrationBean implements Serializable{

    @EJB
    private UserManager um;
    private User user;
    private Schedule schedule;

    public RegistrationBean() {
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Schedule getSchedule() {
        if (schedule == null) {
            schedule = new Schedule();
        }
        return schedule;
    }
    
    public int maxYear(){
        return new Date().getYear() - 10;
    }
    
    public void register() {
        
        String objmail = "Welcome in MeteoCal!";
        
        if(!um.save(user,schedule)){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The email inserted already exists!Please choose another one.","The email inserted already exists! Please choose another one."));
        }
        else{
            String textmail = "Hi "+user.getName()+",<br><br>"+
                                  "Now, you are registered into MeteoCal"+
                                  "<br><br>"+"So, you can login into MeteoCal and manage your events."+"<br><br>"+
                                  "Enjoy yourself :)"+"<br>"+
                                  "<br>Many thanks,<br><i>MeteoCal Staff</i><br><br><br><font size=\"1\" color=\"gray\">"+
                                  "This mail has been sent automatically, so please do not reply to this address</font>";

        
            Mail.sendMail(user.getEmail(), "meteocalstaff@gmail.com",  objmail, textmail);
            user= new User();
            user.setIdSchedule(new Schedule());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration completed! Login into the application to open your homepage!",""));
        }
    }
    public void setSchedule(Schedule schedule) {
        this.schedule = schedule;
    }

    /*public String isPublic(){ // vedi
        if (user.getIdSchedule().getPublic1())
            return "public schedule";
        return "private schedule";
    }*/

    public void click() {
        RequestContext requestContext = RequestContext.getCurrentInstance();
         
        requestContext.update("form:display");
        requestContext.execute("PF('dlg').show()");
    }
    
    public Date dateOfBirth(){
        Date date = new Date();
        date.setYear(date.getYear()-15);
        System.err.println("Dateofbirth--> " + date.toString());
        return date;
    }
}
